﻿using System;
using System.Drawing;
using AccessControl.Common.Enumerations;
using AccessControl.SecurityMonitoring.Struct;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;


namespace AccessControl.SecurityMonitoring.Form
{
    public partial class NotifyForm : XtraForm
    {

        #region Fields

        private readonly FifoBuffer<PersonInfo> _displayList = new FifoBuffer<PersonInfo>(3);

        public Monitoring MonitoringForm { get; set; }


        #endregion

        #region Constructor

        public NotifyForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handler

        private void NotifyForm_Load(object sender, EventArgs e)
        {
            ResizeLabelFonts();
        }

        private void NotifyForm_Resize(object sender, EventArgs e)
        {
            ResizeLabelFonts();
        }

        private void NotifyForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            e.Cancel = true;
            Visible = false;
            if (MonitoringForm == null || !MonitoringForm.checkButtonShowNotifyForm.Checked)
                return;
            MonitoringForm.checkButtonShowNotifyForm.Checked = false;
        }

        #endregion

        #region Method

        public void DiplayPersonInfo(PersonInfo personInfo)
        {
            _displayList.Add(personInfo);
            Invoke(new EventHandler(RefreshDisplay));
        }

        private void RefreshDisplay(object sender, EventArgs e)
        {
            if (_displayList.Count < 1)
                return;

            var firstPerson = _displayList[0];
            pictureEdit1.Image = ByteImageConverter.FromByteArray(firstPerson.Pic);
            labelControlName1.Text = firstPerson.FullName;
            labelControlReason1.Text = firstPerson.Reason;
            labelControlEvent1.Text = firstPerson.EventType;
            if (firstPerson.Result != EventResultConstants.Success)
            {
                if ((firstPerson.Result != EventResultConstants.TimeoutEntrance) || ((firstPerson.Result != EventResultConstants.TimeoutEntrance)))
                {
                    pictureEdit1.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlName1.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlReason1.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlEvent1.BackColor = Color.FromArgb(255, 128, 128);
                }
                else
                {
                    pictureEdit1.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlName1.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlReason1.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlEvent1.BackColor = Color.FromArgb(255, 128, 0);
                }
            }
            else
            {
                if (firstPerson.HasTwineEvent)
                {
                    pictureEdit1.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlName1.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlReason1.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlEvent1.BackColor = Color.FromArgb(128, 128, 255);
                }
                else
                {
                    pictureEdit1.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlName1.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlReason1.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlEvent1.BackColor = Color.FromArgb(128, 255, 128);
                }
            }
            if (_displayList.Count < 2)
                return;
            var secondPerson = _displayList[1];
            pictureEdit2.Image = ByteImageConverter.FromByteArray(secondPerson.Pic);
            labelControlName2.Text = secondPerson.FullName;
            labelControlReason2.Text = secondPerson.Reason;
            labelControlEvent2.Text = secondPerson.EventType;
            if (secondPerson.Result != EventResultConstants.Success)
            {
                if ((secondPerson.Result != EventResultConstants.TimeoutEntrance) || ((secondPerson.Result != EventResultConstants.TimeoutEntrance)))
                {
                    pictureEdit2.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlName2.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlReason2.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlEvent2.BackColor = Color.FromArgb(255, 128, 128);
                }
                else
                {
                    pictureEdit2.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlName2.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlReason2.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlEvent2.BackColor = Color.FromArgb(255, 128, 0);
                }
            }
            else
            {
                if(secondPerson.HasTwineEvent)
                {
                    pictureEdit2.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlName2.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlReason2.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlEvent2.BackColor = Color.FromArgb(128, 128, 255);
                }
                else
                {
                    pictureEdit2.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlName2.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlReason2.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlEvent2.BackColor = Color.FromArgb(128, 255, 128);
                }
            }
            if (_displayList.Count < 3)
                return;
            var thirdPerson = _displayList[2];
            pictureEdit3.Image = ByteImageConverter.FromByteArray(thirdPerson.Pic);
            labelControlName3.Text = thirdPerson.FullName;
            labelControlReason3.Text = thirdPerson.Reason;
            labelControlEvent3.Text = thirdPerson.EventType;
            if (thirdPerson.Result != EventResultConstants.Success)
            {
                if ((thirdPerson.Result != EventResultConstants.TimeoutEntrance) || ((thirdPerson.Result != EventResultConstants.TimeoutEntrance)))
                {
                    pictureEdit3.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlName3.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlReason3.BackColor = Color.FromArgb(255, 128, 128);
                    labelControlEvent3.BackColor = Color.FromArgb(255, 128, 128);
                }
                else
                {
                    pictureEdit3.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlName3.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlReason3.BackColor = Color.FromArgb(255, 128, 0);
                    labelControlEvent3.BackColor = Color.FromArgb(255, 128, 0);
                }
            }
            else
            {
                if (thirdPerson.HasTwineEvent)
                {
                    pictureEdit3.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlName3.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlReason3.BackColor = Color.FromArgb(128, 128, 255);
                    labelControlEvent3.BackColor = Color.FromArgb(128, 128, 255);
                }
                else
                {
                    pictureEdit3.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlName3.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlReason3.BackColor = Color.FromArgb(128, 255, 128);
                    labelControlEvent3.BackColor = Color.FromArgb(128, 255, 128);
                }
            }
        }

        private void ResizeLabelFonts()
        {
            var fontSize = labelControlName1.Height * 40 / 100;

            labelControlName1.Font = new Font(labelControlName1.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlName2.Font = new Font(labelControlName2.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlName3.Font = new Font(labelControlName3.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlReason1.Font = new Font(labelControlReason1.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlReason2.Font = new Font(labelControlReason2.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlReason3.Font = new Font(labelControlReason3.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlEvent1.Font = new Font(labelControlEvent1.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlEvent2.Font = new Font(labelControlEvent2.Font.FontFamily, fontSize, GraphicsUnit.Pixel);
            labelControlEvent3.Font = new Font(labelControlEvent3.Font.FontFamily, fontSize, GraphicsUnit.Pixel);

            labelControlName1.Height = fontSize * 5;
            labelControlName2.Height = fontSize * 5;
            labelControlName3.Height = fontSize * 5;
            labelControlReason1.Height = fontSize * 5;
            labelControlReason2.Height = fontSize * 5;
            labelControlReason3.Height = fontSize * 5;
            labelControlEvent1.Height = fontSize * 5;
            labelControlEvent2.Height = fontSize * 5;
            labelControlEvent3.Height = fontSize * 5;
        }

        #endregion

        private void NotifyForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
        }

    }
}